/**
 * @name Cursor indicator
 * @description Responsible for displaying cursor during drag
 */
jt.module('CursorIndicator', function(context) {

    // useful shorthands
    var canvas = context.canvas,
        settings = context.settings,
        gh = context.settings.grid.h,
        gv = context.settings.grid.v;

    // Private members
    var cGridX = 0,
        cGridY = 0,
        enabled = false,

        options = util.extend({
            enabled: true,
            fill: '#FFF',
            stroke: 'none',
            opacity: 0.2,
            animFill: '#FFF',
            animStroke: 'none',
            animOpacity: 0.3,
            animEasing: 'linear',
            fadeOutTime: 250
        }, settings.CursorIndicator || {}),

        cursor = canvas.rect(0, 0, gh, gv, 0),

        applyCursorAttributes = function() {
            cursor.attr({
                'fill': options.fill,
                'stroke': options.stroke,
                'opacity': options.opacity
            }).hide();

        };

    return {

        events: {

            /*
             *      Summary: Initialize module event handler
             */
            init: function() {
                context.log('[CursorIndicator] init');
                applyCursorAttributes();
            },

            itemDragStart: function(item, gridPos) {
                if (options.enabled !== true) return;

                cGridX = gridPos.x;
                cGridY = gridPos.y;

                cursor.show();
                enabled = true;
            },

            itemDragEnd: function() {
                cursor.hide();
                enabled = false;
            },

            itemDragCollision: function() {
                cursor.hide();
                enabled = false;
            },

            itemCursorShow: function() {
                if (options.enabled !== true) return;

                cursor.show();
                enabled = true;
            },

            itemCursorHide: function() {
                cursor.hide();
                enabled = false;
            },

            itemCursorUpdate: function(cx, cy) {

                if (options.enabled !== true) return;

                var gx = cx / gh,
                    gy = cy / gv;

                if ((cGridX != gx || cGridY != gy) && enabled) {

                    cGridX = gx;
                    cGridY = gy;

                    canvas.rect(cx, cy, gh, gv, 0).attr({
                        'fill': options.animFill,
                        'stroke': options.animStroke,
                        'opacity': options.animOpacity
                    }).toBack().animate({
                        'opacity': 0.0
                    }, options.fadeOutTime, options.animEasing, function() {
                        this.remove();
                    });

                }

                cursor.attr({
                    'x': cx,
                    'y': cy
                });
            },

            attrChange: function(name, value) {
                switch (name) {
                case 'CursorIndicator':
                    util.extend(options, value);
                    applyCursorAttributes();
                    break;
                }
            }
        }
    }

});